﻿using System.Collections.ObjectModel;
using System.Linq;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;

namespace MyMeetup.ViewModels
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class PhotoPageViewModel : ViewModelBase
    {
        private DataService _dataService;
        private int _albumId;

        /// <summary>
        /// Initializes a new instance of the PhotoPageViewModel class.
        /// </summary>
        public PhotoPageViewModel()
        {
            _dataService = new DataService();
            Messenger.Default.Register<PropertyChangedMessage<ObservableCollection<Photo>>>(this,
                                                                        (action) => CurrentPhotos = action.NewValue);

            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real": Connect to service, etc...
            ////}
        }

        public PhotoPageViewModel(int albumId, string current):this()
        {
            _albumId = albumId;
            GetPhotos(current);
        }



        private void GetPhotos(string currentPhotoUrl)
        {
            _dataService.GetAlbumPhotos(_albumId, (x) =>
            {
                DispatcherHelper.Initialize();
                DispatcherHelper.CheckBeginInvokeOnUI(
                    () => SetPhotos(x, currentPhotoUrl));
            }, ResponseHelper.ErrorCallback);
        }


        private void SetPhotos(ObservableCollection<Photo> photos, string currentPhotoUrl)
        {
            CurrentPhotos = photos;
            SelectedPhoto = CurrentPhotos.FirstOrDefault(photo => photo.photo_link == currentPhotoUrl);
        }


        /// <summary>
        /// The <see cref="SelectedPhoto" /> property's name.
        /// </summary>
        public const string SelectedPhotoPropertyName = "SelectedPhoto";

        private Photo _selectedPhoto;

        /// <summary>
        /// Gets the SelectedPhoto property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public Photo SelectedPhoto
        {
            get
            {
                return _selectedPhoto;
            }

            set
            {
                if (_selectedPhoto == value)
                {
                    return;
                }

                var oldValue = _selectedPhoto;
                _selectedPhoto = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(SelectedPhotoPropertyName);

            }
        }

        /// <summary>
        /// The <see cref="CurrentPhotos" /> property's name.
        /// </summary>
        public const string CurrentPhotosPropertyName = "CurrentPhotos";

        private ObservableCollection<Photo> _currentPhotos;

        /// <summary>
        /// Gets the CurrentPhotos property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<Photo> CurrentPhotos
        {
            get
            {
                return _currentPhotos;
            }

            set
            {
                if (_currentPhotos == value)
                {
                    return;
                }

                var oldValue = _currentPhotos;
                _currentPhotos = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(CurrentPhotosPropertyName);

            }
        }

    }
}